
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.middleware.locale &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-middleware-locale">
            
  <h1>Source code for django.middleware.locale</h1><div class="highlight"><pre>
<span></span><span class="s2">&quot;This is the locale selecting middleware that will look at accept headers&quot;</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.conf.urls.i18n</span> <span class="k">import</span> <span class="n">is_language_prefix_patterns_used</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="k">import</span> <span class="n">get_script_prefix</span><span class="p">,</span> <span class="n">is_valid_path</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">translation</span>
<span class="kn">from</span> <span class="nn">django.utils.cache</span> <span class="k">import</span> <span class="n">patch_vary_headers</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="k">import</span> <span class="n">MiddlewareMixin</span>


<div class="viewcode-block" id="LocaleMiddleware"><a class="viewcode-back" href="../../../ref/middleware.html#django.middleware.locale.LocaleMiddleware">[docs]</a><span class="k">class</span> <span class="nc">LocaleMiddleware</span><span class="p">(</span><span class="n">MiddlewareMixin</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This is a very simple middleware that parses a request</span>
<span class="sd">    and decides what translation object to install in the current</span>
<span class="sd">    thread context. This allows pages to be dynamically</span>
<span class="sd">    translated to the language the user desires (if the language</span>
<span class="sd">    is available, of course).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">response_redirect_class</span> <span class="o">=</span> <span class="n">HttpResponseRedirect</span>

    <span class="k">def</span> <span class="nf">process_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="n">urlconf</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;urlconf&#39;</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">ROOT_URLCONF</span><span class="p">)</span>
        <span class="n">i18n_patterns_used</span><span class="p">,</span> <span class="n">prefixed_default_language</span> <span class="o">=</span> <span class="n">is_language_prefix_patterns_used</span><span class="p">(</span><span class="n">urlconf</span><span class="p">)</span>
        <span class="n">language</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language_from_request</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">check_path</span><span class="o">=</span><span class="n">i18n_patterns_used</span><span class="p">)</span>
        <span class="n">language_from_path</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language_from_path</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path_info</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">language_from_path</span> <span class="ow">and</span> <span class="n">i18n_patterns_used</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">prefixed_default_language</span><span class="p">:</span>
            <span class="n">language</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">LANGUAGE_CODE</span>
        <span class="n">translation</span><span class="o">.</span><span class="n">activate</span><span class="p">(</span><span class="n">language</span><span class="p">)</span>
        <span class="n">request</span><span class="o">.</span><span class="n">LANGUAGE_CODE</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">process_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">):</span>
        <span class="n">language</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language</span><span class="p">()</span>
        <span class="n">language_from_path</span> <span class="o">=</span> <span class="n">translation</span><span class="o">.</span><span class="n">get_language_from_path</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">path_info</span><span class="p">)</span>
        <span class="n">urlconf</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;urlconf&#39;</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">ROOT_URLCONF</span><span class="p">)</span>
        <span class="n">i18n_patterns_used</span><span class="p">,</span> <span class="n">prefixed_default_language</span> <span class="o">=</span> <span class="n">is_language_prefix_patterns_used</span><span class="p">(</span><span class="n">urlconf</span><span class="p">)</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span> <span class="o">==</span> <span class="mi">404</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">language_from_path</span> <span class="ow">and</span>
                <span class="n">i18n_patterns_used</span> <span class="ow">and</span> <span class="n">prefixed_default_language</span><span class="p">):</span>
            <span class="c1"># Maybe the language code is missing in the URL? Try adding the</span>
            <span class="c1"># language prefix and redirecting to that URL.</span>
            <span class="n">language_path</span> <span class="o">=</span> <span class="s1">&#39;/</span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">language</span><span class="p">,</span> <span class="n">request</span><span class="o">.</span><span class="n">path_info</span><span class="p">)</span>
            <span class="n">path_valid</span> <span class="o">=</span> <span class="n">is_valid_path</span><span class="p">(</span><span class="n">language_path</span><span class="p">,</span> <span class="n">urlconf</span><span class="p">)</span>
            <span class="n">path_needs_slash</span> <span class="o">=</span> <span class="p">(</span>
                <span class="ow">not</span> <span class="n">path_valid</span> <span class="ow">and</span> <span class="p">(</span>
                    <span class="n">settings</span><span class="o">.</span><span class="n">APPEND_SLASH</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">language_path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span> <span class="ow">and</span>
                    <span class="n">is_valid_path</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">/&#39;</span> <span class="o">%</span> <span class="n">language_path</span><span class="p">,</span> <span class="n">urlconf</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="p">)</span>

            <span class="k">if</span> <span class="n">path_valid</span> <span class="ow">or</span> <span class="n">path_needs_slash</span><span class="p">:</span>
                <span class="n">script_prefix</span> <span class="o">=</span> <span class="n">get_script_prefix</span><span class="p">()</span>
                <span class="c1"># Insert language after the script prefix and before the</span>
                <span class="c1"># rest of the URL</span>
                <span class="n">language_url</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">get_full_path</span><span class="p">(</span><span class="n">force_append_slash</span><span class="o">=</span><span class="n">path_needs_slash</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span>
                    <span class="n">script_prefix</span><span class="p">,</span>
                    <span class="s1">&#39;</span><span class="si">%s%s</span><span class="s1">/&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">script_prefix</span><span class="p">,</span> <span class="n">language</span><span class="p">),</span>
                    <span class="mi">1</span>
                <span class="p">)</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_redirect_class</span><span class="p">(</span><span class="n">language_url</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">i18n_patterns_used</span> <span class="ow">and</span> <span class="n">language_from_path</span><span class="p">):</span>
            <span class="n">patch_vary_headers</span><span class="p">(</span><span class="n">response</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;Accept-Language&#39;</span><span class="p">,))</span>
        <span class="k">if</span> <span class="s1">&#39;Content-Language&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">response</span><span class="p">:</span>
            <span class="n">response</span><span class="p">[</span><span class="s1">&#39;Content-Language&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">language</span>
        <span class="k">return</span> <span class="n">response</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>